package com.demo.backtemplate.service.impl;

import com.demo.backtemplate.mappers.ProductMapper;
import com.demo.backtemplate.models.converter.ProductConverter;
import com.demo.backtemplate.models.dto.ProductDTO;
import com.demo.backtemplate.models.entity.Product;
import com.demo.backtemplate.service.ProductService;
import com.xlf.utility.BaseResponse;
import com.xlf.utility.ErrorCode;
import com.xlf.utility.ResultUtil;
import com.xlf.utility.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProductServiceImpl implements ProductService {
    private final ProductMapper productMapper;

    @Override
    public ResponseEntity<BaseResponse<List<ProductDTO>>> getProductList(Integer page, Integer size) {
        try {
            // 计算偏移量
            int offset = (page - 1) * size;
            
            // 获取分页数据
            List<Product> products = productMapper.findAllWithPagination(offset, size);
            List<ProductDTO> productDTOs = products.stream()
                    .map(ProductConverter::toDTO)
                    .collect(Collectors.toList());
            
            return ResultUtil.success("获取商品列表成功", productDTOs);
        } catch (Exception e) {
            log.error("获取商品列表失败", e);
            throw new BusinessException("获取商品列表失败", ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public ResponseEntity<BaseResponse<ProductDTO>> getProductById(Long id) {
        try {
            Product product = productMapper.findById(id);
            if (product == null) {
                throw new BusinessException("商品不存在", ErrorCode.NOT_EXIST);
            }
            ProductDTO productDTO = ProductConverter.toDTO(product);
            return ResultUtil.success("获取商品详情成功", productDTO);
        } catch (Exception e) {
            log.error("获取商品详情失败", e);
            throw new BusinessException("获取商品详情失败", ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional
    public ResponseEntity<BaseResponse<Void>> createProduct(ProductDTO productDTO) {
        try {
            Product product = ProductConverter.toEntity(productDTO);
            product.setStatus(1); // 默认上架
            // 设置创建时间和更新时间
            LocalDateTime now = LocalDateTime.now();
            product.setCreateTime(now);
            product.setUpdateTime(now);
            
            int result = productMapper.insert(product);
            if (result <= 0) {
                throw new BusinessException("创建商品失败", ErrorCode.OPERATION_FAILED);
            }
            return ResultUtil.success("创建商品成功");
        } catch (Exception e) {
            log.error("创建商品失败", e);
            throw new BusinessException("创建商品失败", ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional
    public ResponseEntity<BaseResponse<Void>> updateProduct(ProductDTO productDTO) {
        try {
            Product product = ProductConverter.toEntity(productDTO);
            // 设置更新时间
            product.setUpdateTime(LocalDateTime.now());
            
            int result = productMapper.update(product);
            if (result <= 0) {
                throw new BusinessException("更新商品失败", ErrorCode.OPERATION_FAILED);
            }
            return ResultUtil.success("更新商品成功");
        } catch (Exception e) {
            log.error("更新商品失败", e);
            throw new BusinessException("更新商品失败", ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional
    public ResponseEntity<BaseResponse<Void>> deleteProduct(Long id) {
        try {
            int result = productMapper.delete(id);
            if (result <= 0) {
                throw new BusinessException("删除商品失败", ErrorCode.OPERATION_FAILED);
            }
            return ResultUtil.success("删除商品成功");
        } catch (Exception e) {
            log.error("删除商品失败", e);
            throw new BusinessException("删除商品失败", ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional
    public ResponseEntity<BaseResponse<Void>> updateProductStatus(Long id, Integer status) {
        try {
            int result = productMapper.updateStatus(id, status);
            if (result <= 0) {
                throw new BusinessException("更新商品状态失败", ErrorCode.OPERATION_FAILED);
            }
            return ResultUtil.success("更新商品状态成功");
        } catch (Exception e) {
            log.error("更新商品状态失败", e);
            throw new BusinessException("更新商品状态失败", ErrorCode.OPERATION_FAILED);
        }
    }
} 